package com.fractalist.base.core;

import java.util.LinkedList;
import java.util.List;

import com.fractalist.base.core.constants.ErrorConstants;



/**
 * 业务逻辑中的异常信息类，将service中的异常信息传递到action的拦截器中。<br>
 * 在页面显示时，通过jstl的国际化标签根据错误码在资源文件中找到匹配的中文进行显示<br>
 * example：throws new ServiceException("user.not.exist")
 * 
 * @author radio
 * 
 */
public class ServiceException extends RuntimeException {

	private static final long serialVersionUID = 8452042078986632665L;

	private int errorCode = 0;

	private String message = "";
	
	private List<Object> paramList = new LinkedList<Object>();
	
	public ServiceException(int errorCode, Object... params) {
		super(String.valueOf(errorCode));
		this.errorCode = errorCode;
		for(Object obj : params){
			paramList.add(obj);
		}
	}
	
	public ServiceException(String message) {
		this.message = message;
	}

	public int getErrorCode() {
		return errorCode;
	}
	
	public String getMessage() {
		if("".equals(message)){
			message = ErrorConstants.ERROR_MAP.get(this.errorCode);
			int len = paramList.size();
			for(int i=0;i<len;i++){
				message = message.replaceAll("\\{"+ i +"\\}", paramList.get(i).toString());
			}
		}
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
